package com.dyz.view;

import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.JButton;

import com.dyz.model.MyDate;

public class Widok extends JFrame {
	/**
	 * nanananananaanna... chuuuuuuj...
	 */
	private static final long serialVersionUID = 7217054161253817809L;

	Calendar calendar = Calendar.getInstance();

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JComboBox<String> cbx1;
	private JComboBox<String> cbx2;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private JMenuItem menuItem1, menuItem2, menuItem3;
	private JComboBox<String> cbx3;
	private JButton btnDodajFakture;
	private JMenuItem menuItem0;
	private JComboBox<Integer> comboVat;
	private JLabel lblNewLabel;
	private JLabel lblInvoice;
	private JLabel lblCompany;
	private JLabel lblAdress;
	private JLabel lblDescription;
	private JLabel lblPrice;
	private JLabel lblVat;
	private JPanel panel;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// Widok frame = new Widok();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/*
	 * View singleton.
	 */
	
	private static Widok instance;

	private Widok() {

		contentPane = new JPanel();

		panel = new JPanel();
		panel.setBackground(UIManager.getColor("CheckBox.background"));
		panel.setBounds(0, 0, 760, 305);
		panel.setLayout(null);

		elements();

	}

	public static Widok getSingletonInstance() {
		if(instance == null) {
			instance = new Widok();
		}
		return instance;
	}

	public JPanel getPanel() {
		return panel;
	}

	public void removeAlll() {
		this.getContentPane().removeAll();
		this.repaint();
	}

	public void elements() {
		textFields();
		buttons();

		comboBoxes();
		labels();
		this.repaint();
	}

	private void textFields() {

		new MyDate(calendar.get(Calendar.DAY_OF_MONTH),
				calendar.get(Calendar.MONTH) + 1);
		textField = new JTextField();
		textField.setBounds(10, 41, 86, 20);
		textField.setText(String.format("%s-%s-%d", MyDate.day, MyDate.month,
				calendar.get(Calendar.YEAR)));
		panel.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(106, 41, 86, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(202, 41, 86, 20);
		panel.add(textField_2);
		textField_2.setColumns(10);

		textField_3 = new JTextField();
		textField_3.setBounds(298, 41, 86, 20);
		panel.add(textField_3);
		textField_3.setColumns(10);

		textField_4 = new JTextField();
		textField_4.setBounds(394, 41, 86, 20);
		panel.add(textField_4);
		textField_4.setColumns(10);

		textField_5 = new JTextField();
		textField_5.setBounds(490, 41, 86, 20);
		panel.add(textField_5);
		textField_5.setColumns(10);

		textField_6 = new JTextField();
		textField_6.setBounds(490, 72, 86, 20);
		textField_6.setText("0");
		panel.add(textField_6);
		textField_6.setColumns(10);

	}

	private void buttons() {
		rdbtnNewRadioButton = new JRadioButton("koszt");
		rdbtnNewRadioButton.setBounds(10, 72, 109, 23);
		panel.add(rdbtnNewRadioButton);

		rdbtnNewRadioButton_1 = new JRadioButton("przychod");
		rdbtnNewRadioButton_1.setBounds(10, 102, 109, 23);
		panel.add(rdbtnNewRadioButton_1);

		addToButtonGroup(rdbtnNewRadioButton, rdbtnNewRadioButton_1);

		btnDodajFakture = new JButton("Dodaj fakture");
		btnDodajFakture.setBounds(478, 103, 115, 23);
		panel.add(btnDodajFakture);
	}

	private void menus() {
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menuPlik = new JMenu("File");
		menuBar.add(menuPlik);

		menuItem0 = new JMenuItem("New");
		menuItem0.setActionCommand("new");
		menuPlik.add(menuItem0);

		menuItem1 = new JMenuItem("open");
		menuItem1.setActionCommand("open");
		menuPlik.add(menuItem1);

		menuItem2 = new JMenuItem("save");
		menuItem2.setActionCommand("save");

		menuItem3 = new JMenuItem("close");
		menuItem3.setActionCommand("close");
		menuPlik.add(menuItem3);
	}

	private void comboBoxes() {
		cbx1 = new JComboBox<String>();
		cbx1.setBounds(202, 72, 86, 20);
		cbx1.setActionCommand("firma");
		cbx1.addItem("");

		panel.add(cbx1);

		cbx2 = new JComboBox<String>();
		cbx2.setBounds(298, 72, 86, 20);
		cbx2.setActionCommand("adres");
		cbx2.addItem("");
		panel.add(cbx2);

		JLabel lblWydrukZMiesiaca = new JLabel("Wydruk z miesiaca");
		lblWydrukZMiesiaca.setBounds(10, 150, 109, 23);
		panel.add(lblWydrukZMiesiaca);

		cbx3 = new JComboBox<String>();
		cbx3.setBounds(129, 151, 86, 20);
		cbx3.setActionCommand("miesiac");
		panel.add(cbx3);

		addMonths();

		comboVat = new JComboBox<Integer>();
		comboVat.setBounds(601, 41, 74, 20);
		comboVat.addItem(23);
		comboVat.addItem(7);
		comboVat.addItem(5);
		comboVat.addItem(0);
		panel.add(comboVat);

	}

	private void labels() {
		lblNewLabel = new JLabel("Date");
		lblNewLabel.setBounds(10, 11, 86, 19);
		panel.add(lblNewLabel);

		lblInvoice = new JLabel("Invoice number");
		lblInvoice.setBounds(106, 13, 86, 19);
		panel.add(lblInvoice);

		lblCompany = new JLabel("Company");
		lblCompany.setBounds(202, 13, 86, 19);
		panel.add(lblCompany);

		lblAdress = new JLabel("Adress");
		lblAdress.setBounds(298, 11, 86, 19);
		panel.add(lblAdress);

		lblDescription = new JLabel("Description");
		lblDescription.setBounds(394, 11, 86, 19);
		panel.add(lblDescription);

		lblPrice = new JLabel("Price");
		lblPrice.setBounds(490, 13, 86, 19);
		panel.add(lblPrice);

		lblVat = new JLabel("Vat");
		lblVat.setBounds(602, 13, 86, 19);
		panel.add(lblVat);
	}

	public void addToCbx1(String s) {
		cbx1.addItem(s);
	}

	public void addToCbx2(String s) {
		cbx2.addItem(s);
	}

	public void clearCbx() {
		cbx1.removeAllItems();
		cbx2.removeAllItems();
		addToCbx1("");
		addToCbx2("");
	}

	public String radioState() {

		if (rdbtnNewRadioButton.isSelected())
			return "koszt";
		else if (rdbtnNewRadioButton_1.isSelected())
			return "przychod";
		else
			return "error";

	}

	public String[] getTxt() {

		String s[] = { textField.getText(), textField_1.getText(),
				textField_2.getText(), textField_3.getText(),
				textField_4.getText(), textField_5.getText(),
				textField_6.getText() };
		return s;
	}

	public void setTxt1(String s) {
		textField.setText(s);
	}

	public void setTxt2(String s) {
		textField_1.setText(s);
	}

	public void setTxt3(String s) {
		textField_2.setText(s);
	}

	public void setTxt4(String s) {
		textField_3.setText(s);
	}

	public void setTxt5(String s) {
		textField_4.setText(s);
	}

	public void setTxt6(String s) {
		textField_5.setText(s);
	}

	public void setTxt7(String s) {
		textField_6.setText(s);
	}

	public void addButtonsListener(ActionListener l) {

		btnDodajFakture.addActionListener(l);

	}

	public void addComboListener(ActionListener l) {
		cbx1.addActionListener(l);
		cbx2.addActionListener(l);
		cbx3.addActionListener(l);
	}

	public void addMonths() {

		cbx3.addItem("");
		cbx3.addItem("january");
		cbx3.addItem("february");
		cbx3.addItem("march");
		cbx3.addItem("april");
		cbx3.addItem("may");
		cbx3.addItem("june");
		cbx3.addItem("july");
		cbx3.addItem("august");
		cbx3.addItem("september");
		cbx3.addItem("october");
		cbx3.addItem("november");
		cbx3.addItem("december");

	}

	public void addToButtonGroup(JRadioButton... rb) {
		ButtonGroup bg = new ButtonGroup();
		for (JRadioButton j : rb) {
			bg.add(j);
		}
	}

	public void wyczysc() {
		// setTxt1("");
		setTxt2("");
		setTxt3("");
		setTxt4("");
		setTxt5("");
		setTxt6("");
		setTxt7("0");
	}

	public int getVat() {
		return (int) comboVat.getSelectedItem();
	}

	public void setWindowName(String name) {
		this.setTitle(name);
	}
}
